import { EventKey } from "../common/Key";
import { GameData } from "./GameData";
import { EnergyMgr } from "../component/EnergyMgr";
import { TestLevel } from "./GameDefine";
var GameAudioMgr = require('GameAudioMgr');
var SceneGame = cc.Class({
    extends: require('BaseScene'),

    statics: {
        instance: null,
    },

    properties: {
        pfbItemCell: cc.Prefab,
        pfbItemKnot: cc.Prefab,
        pfbItemSelectEffect: cc.Prefab,
        nodBtnNext: cc.Node,
        boardMgr: require('BoardMgr'),
        labLevel: cc.Label,
        nodBtnBegin: cc.Node,
        nodBtnSucc: cc.Node,
        nodBtnCheckOver: cc.Node,
    },

    onLoad() {
        this._super();
        SceneGame.instance = this;
        this._initNodePool();
        this._registeEvent();
    },

    start() {
        G.partner.sendEvent('场景', { '类型': 'game' });
        GameAudioMgr.instance.playBgm();

        this._loadLevelJson(() => {
            this.boardMgr.startGame();
        });
        this._updateLevel();
        this._initHideBtn();
    },
    onDestroy() {
        this._unRegisteEvent();
    },
    _startNextLevel() {
        this.nodBtnNext.active = false;
        this._loadLevelJson(() => {
            this.boardMgr.startNextGame();
        });
        this._updateLevel();
    },

    _initHideBtn(){
        if(TestLevel){
            this.nodBtnBegin.active = true;
            this.nodBtnSucc.active = true;
            this.nodBtnCheckOver.active = true;
        }
    },

    _updateLevel() {
        this.labLevel.string = GameData.level + 1 + '';
    },
    // 初始化节点池
    _initNodePool() {
        G.utils.createPool('nodePoolCell', this.pfbItemCell, 4);
        G.utils.createPool('nodePoolKnot', this.pfbItemKnot, 5);
        G.utils.createPool('nodePoolSelectEffect', this.pfbItemSelectEffect, 1);


    },
    _registeEvent() {
        G.onFire.on(EventKey.EVENT_GAME_OVER, this._onFire, this);
    },
    _unRegisteEvent() {
        G.onFire.off(EventKey.EVENT_GAME_OVER, this._onFire, this);
    },
    _onFire(eventName, param) {
        if (eventName == EventKey.EVENT_GAME_OVER) {
            let canUp = GameData.addLevel();
            if (!canUp) {
                G.game.toast('已经是最后一关了', 0.5)
                return;
            }
            this.nodBtnNext.active = true;
            this.nodBtnNext.opacity = 0;
            cc.tween(this.nodBtnNext)
                .to(1, { opacity: 255 })
                .start();
        }
    },

    _loadLevelJson(cb) {
        G.json.addItem(GameData.level + '');
        G.json.startLoad(() => {
            cb && cb();
        });
    },

    buttonListener: function (button) {
        let name = button.node.name;
        if (name == "btnPause") {

        } else if (name == "btnNextLevel") {
            let result = EnergyMgr.subEnergy();
            if (!result) {
                G.game.toast('能量不足', 0.5)
                return;
            }
            this._startNextLevel();
        } else if (name == "btnBack") {
            GameAudioMgr.instance.stopBgm();
            G.audio.clearBgm();
            G.game.loadScene('SceneHome');
        } else if (name == "btnBegin") {
            this.boardMgr.turnBegin();
            this._startNextLevel();
        } else if (name == "btnSucc") {
            this.boardMgr.turnSucc();
        } else if (name == "btnCheckOver") {
            this.boardMgr.checkOver();
        }
    },

    // update (dt) {},
});
